package com.celan.year2023.month01.day09;

public class Solution {
    /**
     * leetcode1806
     */
    public int reinitializePermutation(int n) {
        int[] perm = new int[n];
        for (int i = 0; i < n; i++) {
            perm[i] = i;
        }
        int cnt = 0;
        int[] pre = perm;
        while (true) {
            cnt++;
            int[] arr = operation(n, pre);
            pre = arr;
            if (compare(arr, perm)) return cnt;
        }
    }

    private boolean compare(int[] arr, int[] perm) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != perm[i]) return false;
        }
        return true;
    }

    int[] operation(int n, int[] pre) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            if (i % 2 == 0) arr[i] = pre[i / 2];
            else arr[i] = pre[n / 2 + (i - 1) / 2];
        }
        return arr;
    }
}
